﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmNewGRN : Form
    {
        DataTable LvTable2 = new DataTable("Details");
        double GvTotal = 0.00;
        public frmNewGRN()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvApprovedPo.Columns.Insert(0, check);

            DataGridViewCheckBoxColumn check2 = new DataGridViewCheckBoxColumn();
            check2.HeaderText = "Check Item";
            check2.FalseValue = "0";
            check2.TrueValue = "1";
            check2.Width = 20;
            dgvPODetails.Columns.Insert(0, check2);
        }

        private void frmNewGRN_Load(object sender, EventArgs e)
        {
            ApprovedPRFillGrid();
        }

        private void ApprovedPRFillGrid()
        {

            //Fill the DataGrid
            Purchase_Order LvPO = new Purchase_Order();
            //LvPR.status = "APPROVED";
            List<Purchase_Order> LvList = Purchase_OrderDAO.GetAllApprovedPO();

            DataTable LvTable = new DataTable();
            dgvApprovedPo.DataSource = LvTable;

            LvTable.Columns.Add("POID ");
            LvTable.Columns.Add("BranchID ");
            LvTable.Columns.Add("SupID ");
            LvTable.Columns.Add("PODate ");
            

            foreach (Purchase_Order LvPo2 in LvList)
            {
                LvTable = (DataTable)dgvApprovedPo.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPo2.Po_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPo2.Branch_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPo2.Sup_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPo2.Po_Date.ToString();
                


            }
        }

        private void PODetailFillGrid(string PPoId)
        {


            List<Purchase_Order> LvList = Purchase_OrderDAO.GetApprovedPoDetailsByPoId(PPoId);

            DataTable LvTable = new DataTable();
            dgvPODetails.DataSource = LvTable;

            LvTable.Columns.Add("POID ");
            LvTable.Columns.Add("PLID ");
            LvTable.Columns.Add("Description ");
            LvTable.Columns.Add("Qty ");
            LvTable.Columns.Add("PurchasePrice ");

            foreach (Purchase_Order LvPo in LvList)
            {
                LvTable = (DataTable)dgvPODetails.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPo.Po_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPo.PL_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPo.Description.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPo.Qty.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvPo.status.ToString();


            }
        }

        private void dgvApprovedPo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvApprovedPo.Rows[dgvApprovedPo.CurrentRow.Index].Cells[0];

            string Lvtest = dgvApprovedPo.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtBranchId.Text = dgvApprovedPo.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtSupId.Text = dgvApprovedPo.Rows[e.RowIndex].Cells[3].Value.ToString();
            txtPOId.Text = dgvApprovedPo.Rows[e.RowIndex].Cells[1].Value.ToString();
            List<Supplier> LvList = SupplierDAO.GetSupplierBySupID(txtSupId.Text.ToString());
            foreach (Supplier LvSupName in LvList)
            {

                txtSupName.Text =  LvSupName.Sup_Name.ToString();

            }
           

            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":
                    // PRDetailsFillGrid(Lvtest);
                    //ch1.Value = false;

                    break;

                case "False":
                    PODetailFillGrid(Lvtest);
                    //LvList2.Add(Lvtest);

                    //ch1.Value = true;

                    break;

            }


        }

        private void dgvPODetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvPODetails.Rows[dgvPODetails.CurrentRow.Index].Cells[0];

            string Lvtest = dgvPODetails.Rows[e.RowIndex].Cells[1].Value.ToString();


            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":
                    // PRDetailsFillGrid(Lvtest);
                    //ch1.Value = false;

                    break;

                case "False":
                    SelectedPODetails(Lvtest);
                    //LvList2.Add(Lvtest);

                    //ch1.Value = true;

                    break;

            }
        }

        private void SelectedPODetails(string PPoId)
        {


            List<Purchase_Order> LvList = Purchase_OrderDAO.GetApprovedPoDetailsByPoId(PPoId);

           
            dgvGrnItems.DataSource = LvTable2;

            LvTable2.Columns.Add("POID ");
            LvTable2.Columns.Add("PLID ");
            LvTable2.Columns.Add("Description ");
            LvTable2.Columns.Add("Qty ");
            LvTable2.Columns.Add("PurchasePrice ");

            foreach (Purchase_Order LvPo in LvList)
            {
                LvTable2 = (DataTable)dgvGrnItems.DataSource;

                LvTable2.Rows.Add(LvTable2.NewRow());

                LvTable2.Rows[LvTable2.Rows.Count - 1][0] = LvPo.Po_Id.ToString();
                LvTable2.Rows[LvTable2.Rows.Count - 1][1] = LvPo.PL_Id.ToString();
                LvTable2.Rows[LvTable2.Rows.Count - 1][2] = LvPo.Description.ToString();
                LvTable2.Rows[LvTable2.Rows.Count - 1][3] = LvPo.Qty.ToString();
                LvTable2.Rows[LvTable2.Rows.Count - 1][4] = LvPo.status.ToString();

            }
        }

        private void textBox63_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtGrnNo.Text = GRNADO.GetNewGRNId().ToString();
            //getting the sumof qty

            for (int i = 0; i < dgvGrnItems.RowCount - 1; i++)
            {
                double amount = double.Parse(dgvGrnItems.Rows[i].Cells[4].Value.ToString()) * double.Parse(dgvGrnItems.Rows[i].Cells[3].Value.ToString());
                GvTotal += amount;

            }
            txtTotal.Text = GvTotal.ToString();
        }

        ///Saving the GRN Details

        private int SaveGRN(string xmlstring)
        {
            string Lvxml = xmlstring;

            GRN LvGRN = CollectGRNFigures();
            if (LvGRN != null)
            {
                return GRNADO.SaveGRN(LvGRN, Lvxml);
            }
            else
            {
                return 0;
            }
        }

        private GRN CollectGRNFigures()
        {

            try
            {

                GRN LvGRNMaster = new GRN();

                LvGRNMaster.GRN_Id = txtGrnNo.Text.ToString();
                LvGRNMaster.Branch_Id = txtBranchId.Text.ToString();
                LvGRNMaster.Sup_ID = txtSupId.Text.ToString();
                LvGRNMaster.PO_Id = txtPOId.Text.ToString();
                LvGRNMaster.Total = decimal.Parse(txtTotal.Text.ToString());
                LvGRNMaster.Sup_Bill_No = txtSupBillNo.Text.ToString();
                LvGRNMaster.Book_GRNno = txtBookGrn.Text.ToString();
                LvGRNMaster.User_Id = "1";
                LvGRNMaster.Date_Create = DateTime.Now;


                return LvGRNMaster;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet("grndetails");

            string detailsXML = "";

            ds.Tables.Add(LvTable2.Copy());
            detailsXML = ds.GetXml();

            SaveGRN(detailsXML);
            MessageBox.Show("GRN Was Succesfuly Saved");
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmReportViewer frmR = new frmReportViewer(3, txtGrnNo.Text.ToString());
            frmR.ShowDialog();
        }

        private void btnNewItem_Click(object sender, EventArgs e)
        {
            frmItem item = new frmItem();
            item.ShowDialog();
        }
    }
}
